package com.ipkiss.incomespringboot.common;

import lombok.Getter;
import lombok.Setter;

/**
 * @author WuYongheng
 * @date 2024/5/12
 * @describe
 */
@Getter
@Setter
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    /**
     * 操作成功的响应
     *
     * @return 成功结果对象
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(Constants.SUCCESS_CODE);
        result.setMsg(Constants.SUCCESS_MSG);
        return result;
    }

    /**
     * 带自定义消息和数据的成功响应
     *
     * @param msg  成功消息
     * @param data 成功时返回的数据
     * @param <T>  数据类型
     * @return 成功结果对象
     */
    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = new Result<>(data);
        result.setCode(Constants.SUCCESS_CODE);
        result.setMsg(msg);
        return result;
    }

    /**
     * 操作失败的响应
     *
     * @param msg 错误消息
     * @return 失败结果对象
     */
    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<>();
        result.setCode(Constants.FAILED_CODE);
        result.setMsg(msg);
        return result;
    }
}

